package com.lxm.ymcc.service.impl;
import com.lxm.ymcc.dto.TroDto;
import com.lxm.ymcc.service.ITrollerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class TrollerServiceImpl implements ITrollerService {
    @Autowired
  private RedisTemplate redisTemplate;


    @Override
    public void save(TroDto troDto) {
        Long userId = troDto.getUserId();
        String s = userId.toString();
        Long goodsId = troDto.getGoodsId();
        Integer goodsNum = troDto.getGoodsNum();

        HashMap<Integer, Date> map = new HashMap<>();
        map.put(goodsNum,new Date());
        redisTemplate.opsForHash().put("cart:" +s,"goodsId:" + goodsId,map);




    }

    public List<TroDto> oneById(Long userId) {
        //HashMap<String,Object> map=new HashMap<>();
        Map<Object, Object> entries = redisTemplate.opsForHash().entries("cart:" + userId);
        System.out.println(entries);
        List<TroDto> troDtos = new ArrayList<>();
        for(Object key : entries.keySet()){
            Object value = entries.get(key.toString());
            System.out.println(key+":"+value);
            TroDto troDto = (TroDto)value;
            troDtos.add(troDto);
        }
        //products.add(o);
        return troDtos;
    }


    @Override
    public void delete(Long userId, Long goodsId) {
        //删除goodsId
        redisTemplate.opsForHash().delete("cart:"+userId, goodsId.toString());
        System.out.println("cart：" + redisTemplate.opsForHash().entries("cart:"+userId));

    }
}
